package com.zhanpple.color_picker.library.circle

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import com.zhanpple.color_picker.library.ScreenSizeUtils

/**
 * @author zhangmingping
 * @date 2022/2/22 11:04 上午
 * des:V明度选择器
 */
class ColorValuePicker @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val mPaint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val mLinePaint = Paint(Paint.ANTI_ALIAS_FLAG)

    private val mHalfLightWidth by lazy {
        ScreenSizeUtils.dp2px(2F, resources)
    }

    private val mPaddingV by lazy {
        ScreenSizeUtils.dp2px(5F, resources)
    }
    private var mDy = 0F

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mDy = measuredHeight - mPaddingV * 2
        setColorShader(mFloatArray[0], mFloatArray[1])
        invalidate()
    }

    private fun setColorShader(hua: Float, sat: Float) {
        val intArrayOf = intArrayOf(
            Color.HSVToColor(floatArrayOf(hua, sat, 0F)),
            Color.HSVToColor(floatArrayOf(hua, sat, 1F))
        )
        val linearShader = LinearGradient(
            0F,
            measuredHeight.toFloat() - mPaddingV,
            0F,
            mPaddingV,
            intArrayOf,
            null,
            Shader.TileMode.CLAMP
        )
        mPaint.shader = linearShader
    }


    private var mFloatArray = FloatArray(3)

    fun setCurrentColor(color: Int, useValue: Boolean = false, back: Boolean = false) {
        val floatArray = FloatArray(3)
        Color.colorToHSV(color, floatArray)
        if (mFloatArray[0] != floatArray[0] || mFloatArray[1] != floatArray[1]) {
            setColorShader(floatArray[0], floatArray[1])
        }
        mFloatArray[0] = floatArray[0]
        mFloatArray[1] = floatArray[1]
        if (useValue) {
            mFloatArray[2] = floatArray[2]
        }
        if (back) {
            mColorValueBack?.invoke(mFloatArray[2], Color.HSVToColor(mFloatArray))
        }
        invalidate()
    }


    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        val y = event.y - mPaddingV
        mFloatArray[2] = 1 - (y / mDy).coerceAtMost(1F).coerceAtLeast(0F)
        mColorValueBack?.invoke(mFloatArray[2], Color.HSVToColor(mFloatArray))
        invalidate()
        return true
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.drawRect(0F, 0F, measuredWidth.toFloat(), measuredHeight.toFloat(), mPaint)
        mLinePaint.color = Color.WHITE
        canvas?.drawRect(
            0F,
            (1 - mFloatArray[2]) * mDy - mHalfLightWidth + mPaddingV,
            measuredWidth.toFloat(),
            (1 - mFloatArray[2]) * mDy + mHalfLightWidth + mPaddingV,
            mLinePaint
        )

    }

    var mColorValueBack: ((Float, Int) -> Unit)? = null

    init {
        setLayerType(LAYER_TYPE_SOFTWARE, null)
        Color.colorToHSV(Color.RED, mFloatArray)
    }
}